package com.yb.project.base.service.impl;

import java.util.Date;
import java.util.List;

import com.yb.common.utils.SnowflakeIdWorker;
import com.yb.framework.security.utils.SecurityUtil;
import com.yb.project.base.domain.BaseEntity;
import com.yb.project.base.mapper.BaseMapper;
import com.yb.project.base.service.BaseService;

public abstract class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {
	
	protected abstract BaseMapper<T> getMapper();
	
	public int insert(T entity) {
		// 雪花算法生成ID
		if(entity.getId() == null) {
			entity.setId(SnowflakeIdWorker.getInstance().getId());
		}
		entity.setCreateBy(SecurityUtil.getUsername());
		entity.setCreateTime(new Date());
		return this.getMapper().insert(entity);
	}
	
	public int update(T entity) {
		entity.setUpdateBy(SecurityUtil.getUsername());
		entity.setUpdateTime(new Date());
		return this.getMapper().update(entity);
	}
	
	public int deleteById(Long id) {
		return this.getMapper().deleteById(id);
	}
	
	public int deleteListByIds(Long[] ids) {
		return this.getMapper().deleteListByIds(ids);
	}
	
	public List<T> getAll() {
		return this.getMapper().getAll();
	}
	
	public T getByEntity(T entity) {
		return this.getMapper().getByEntity(entity);
	}
	
	public List<T> getListByEntity(T entity) {
		return this.getMapper().getListByEntity(entity);
	}
	
	public List<T> getPageByEntity(T entity) {
		return this.getMapper().getPageByEntity(entity);
	}
	
	public T getById(Long id) {
		return this.getMapper().getById(id);
	}
	
	public List<T> getListByIds(Long[] ids) {
		return this.getMapper().getListByIds(ids);
	}
	
	public List<String> getNamesByIds(Long[] ids) {
		return this.getMapper().getNamesByIds(ids);
	}
}
